import {Component, OnInit} from '@angular/core';
import {OperateSericve} from '../operate-user.service';
import {LoadingService} from '../../../shared/loading.service';
import {AlertService} from '../../../shared/alert.service';
import {Md5} from 'ts-md5/dist/md5';

@Component({
    selector: 'app-operate-user-list',
    templateUrl: './operate-user-list.component.html',
    styleUrls: ['./operate-user-list.component.scss']
})
export class OperateUserListComponent implements OnInit {
    public accountArr: Array<any> = [];
    // 查询数据
    public conditionsData = {
        key: '2',
        conditions: '',
        importString: '请输入运营人员账号',
    };
    // 保存第几页
    public nowpages: Number = 1;
    public isNodata: Boolean = false;
    public totalItems: any; // 列表数据总数
    public currentPage: any = 1; // 默认的开始页数
    public maxSize: Number = 3; // 显示的最大按钮数量
    public delectId: number;
    // 区分是删除还是重置密码
    public isdelect: Boolean = false;
    // 模态框
    public modalText: string;
    public delectModal: Boolean = false;
    public issuccess: Boolean = false;
    public modelBody: string;
    //   保存密码
    public password: string;
    public userName: any = '';
    public phone: any = '';
    public keys: any = '2';

    constructor(public loading: LoadingService,
                private service: OperateSericve,
                public alert: AlertService) {
    }

    ngOnInit() {
        this.getdata();
    }

    // 获取数据
    getdata() {
        this.loading.show();
        this.password = '123456';
        const params: any = {
            username: this.userName,
            page: this.nowpages,
        };
        this.service.getUsers(params).subscribe(
            res => {
                if (res['data']['length'] === 0) {
                    this.isNodata = true;
                    this.accountArr = [];
                } else {
                    this.accountArr = res['data'];
                    this.totalItems = res['count'];
                    for (let i = 0; i < this.accountArr.length; i++) {
                        this.accountArr[i]['date_joined'] = this.accountArr[i]['date_joined'].replace('T', '  ');
                        const c = this.accountArr[i]['date_joined'].lastIndexOf('.');
                        this.accountArr[i]['date_joined'] = this.accountArr[i]['date_joined'].substring(0, c);
                    }
                }
                console.error(this.accountArr, '获取的数据');
                setTimeout(() => {
                    this.loading.hide();
                }, 300);
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }


    markedWords() {
        this.conditionsData.conditions = '';
        switch (this.conditionsData.key) {
            case '1':
                this.conditionsData.importString = '请输入运营人员姓名';
                break;
            case '2':
                this.conditionsData.importString = '请输入运营人员账号';
                break;
        }
    }

    // 查询按钮
    queryData() {
        this.loading.show();
        const params: any = {
            username: this.userName,
            page: this.nowpages
        };
        this.service.getUsers(params).subscribe(
            res => {
                setTimeout(() => {
                    this.loading.hide();
                }, 300);
                if (res['data']['length'] === 0) {
                    this.isNodata = true;
                    this.accountArr = [];
                } else {
                    this.accountArr = res['data'];
                    this.isNodata = false;
                }
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }

    // 删除那妞
    deleteUser(id: number) {
        this.delectId = id;
        this.modalText = '确定要删除';
        this.isdelect = true;
    }

    // 重置密码
    resetPassword(id: number) {
        this.delectId = id;
        this.modalText = '确定要重置密码';
        this.isdelect = false;
    }

    determination() {
        this.loading.show();
        if (this.isdelect === true) {
            const params: any = {
                // token: localStorage.token,
                admin_id: this.delectId
            };
            this.service.deleteUsers(params).subscribe(
                res => {
                    setTimeout(() => {
                        this.loading.hide();
                    }, 300);
                    // if (res['code'] === 0) {
                    //     this.modelBody = '删除成功';
                    //     this.delectModal = true;
                    //     this.issuccess = true;
                    // } else {
                    //     this.modelBody = '删除失败';
                    //     this.delectModal = true;
                    //     this.issuccess = false;
                    // }
                    this.getdata();

                },
                error => {
                    this.loading.hide();
                    this.alert.show('网络错误');
                }
            );
        }
        if (this.isdelect === false) {
            const params: any = {
                // id: this.delectId,
                // password: Md5.hashStr(this.password),
                username: localStorage.userName
            };
            this.service.resetUsers(params).subscribe(
                res => {
                    setTimeout(() => {
                        this.loading.hide();
                    }, 300);
                    if (res['code'] === 0) {
                        this.modelBody = '密码重置成功';
                        this.delectModal = true;
                        this.issuccess = true;
                    } else {
                        this.modelBody = '密码重置失败';
                        this.delectModal = true;
                        this.issuccess = false;
                    }
                },
                error => {
                    this.loading.hide();
                    this.alert.show('网络错误');
                }
            );
        }
    }

    pageChanged(event) {
        this.loading.show();
        this.nowpages = event.page;
        const params: any = {
            token: localStorage.token,
            page: event.page,
        };
        this.service.getUsers(params).subscribe(
            res => {
                if (res['data']['length'] === 0) {
                    this.isNodata = true;
                    this.accountArr = [];
                } else {
                    this.accountArr = res['data'];
                    this.totalItems = res['count'];
                }
                setTimeout(() => {
                    this.loading.hide();
                }, 300);
            },
            error => {
                this.loading.hide();
                this.alert.show('网络错误');
            }
        );
    }

    confirmshutdown() {
        this.delectModal = false;
        this.loading.show();
        if (this.issuccess === true && this.isdelect === true) {
            const params: any = {
                token: localStorage.token,
                page: this.nowpages,
            };
            this.service.getUsers(params).subscribe(
                res => {
                    if (res['data']['length'] === 0) {
                        this.isNodata = true;
                        this.accountArr = [];
                    } else {
                        this.accountArr = res['data'];
                        this.totalItems = res['count'];
                    }
                    setTimeout(() => {
                        this.loading.hide();
                    }, 300);
                },
                error => {
                    this.loading.hide();
                    this.alert.show('网络错误');
                }
            );
        }
        this.loading.hide();
    }
}
